FROM {{ namespace }}/{{ image_prefix }}openstack-base:{{ tag }}
{% block labels %}
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
{% endblock %}

{% block bifrost_base_header %}{% endblock %}

{% import "macros.j2" as macros with context %}

{{ macros.configure_user(name='bifrost') }}

{# NOTE(mgoddard): EPEL required for nginx #}
{{ macros.enable_extra_repos(['epel']) }}

{% if install_type == 'binary' %}

RUN echo '{{ install_type }} not yet available for {{ base_distro }}' && /bin/false

{% elif install_type == 'source' %}

ENV VENV /var/lib/kolla/venv

{% set bifrost_base_pip_packages = [
    '/bifrost'
] %}

ADD bifrost-base-archive /bifrost-base-source
COPY build_arg.yml /tmp/build_arg.yml
RUN ln -s bifrost-base-source/* bifrost \
    && {{ macros.install_pip(bifrost_base_pip_packages | customizable("pip_packages")) }}

WORKDIR /bifrost

{% if base_package_type == 'rpm' %}
{# Remove updated RabbitMQ packages since these are not supported by Bifrost yet. #}
RUN rm -f /etc/yum.repos.d/rabbitmq_rabbitmq-server.repo \
    && dnf clean all &&  rm -rf /var/dnf/cache
{% elif base_package_type == 'deb' %}
RUN sed -e "s/.*mariadb.*//Ig" -e "s/.*rabbitmq.*//Ig" -i /etc/apt/sources.list \
    && apt-get purge -y mariadb* mysql*
{% endif %}

{# ANSIBLE_GATHER_TIMEOUT is a workaround for https://github.com/ansible/ansible/issues/43884 #}
ENV ANSIBLE_GATHER_TIMEOUT=30

{% block bifrost_ansible_install %}
{%- if base_package_type == 'deb' %}
RUN apt-get update && \
{%- else %}
RUN echo " " && \
{%- endif %}
    bash -c './scripts/env-setup.sh && \
    ansible-playbook -vvvv -i /bifrost/playbooks/inventory/target /bifrost/playbooks/install.yaml \
    -e @/tmp/build_arg.yml && \
{%- if base_package_type == 'deb' %}
    apt-get clean && rm -rf /var/lib/apt/lists/*'
{%- else %}
    dnf clean all && rm -rf /var/dnf/cache'
{%- endif %}
{% endblock %}
{% endif %}

COPY bifrost_sudoers /etc/sudoers.d/kolla_bifrost_sudoers

RUN chmod 750 /etc/sudoers.d \
    && chmod 440 /etc/sudoers.d/kolla_bifrost_sudoers \
    && chown -R bifrost:bifrost /bifrost

{% block bifrost_base_footer %}{% endblock %}
